package com.mashen.action;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.mashensoft.User;
import com.opensymphony.xwork2.ActionSupport;

@Namespace("/mashen")
@Action(value = "user1")
@Results({ @Result(name = "success", location = "/UserMsg.jsp"), @Result(name = "input", location = "/UserMsg.jsp") })
public class UserAction extends ActionSupport {
	private User user;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

//	@Override
//	public void validate() {
//		if ("".equals(user.getName()) || "".equals(user.getPassword()) || "".equals(user.getAge())
//				|| user.getAge() == 0) {
//			addFieldError("error", "参数不完成");
//		}
//		super.validate();
//	}

	@Override
	public String execute() throws Exception {
		System.out.println("第一种：" + user.getName());
		System.out.println("第一种：" + user.getPassword());
		System.out.println("第一种：" + user.getAge());
		return SUCCESS;
	}

//	public void validateLogin() {
//		if ("".equals(user.getName()) || "".equals(user.getPassword()) || "".equals(user.getAge())
//				|| user.getAge() == 0) {
//			addFieldError("error", "你还不填写东西");
//		}
//		super.validate();
//
//	}

	@Action(value = "login", results = { @Result(name = "input", location = "/UserMsg.jsp") })
	public String login() {
		System.out.println("第二种：" + user.getName());
		System.out.println("第二种：" + user.getPassword());
		System.out.println("第二种：" + user.getAge());
		try {
			 FileInputStream fis =new FileInputStream(user.getFileUrl());
			FileOutputStream out = new FileOutputStream("E://image/123.jpg");
			byte[] b = new byte[(int)user.getFileUrl().length()];
			fis.read(b);
			out.write(b);
			fis.close();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "input";
	}
}
